import pygame
import sys
from bullet import Bullet
from player import Player
import base as base
from background import BackGround


class CoreScene(base.BaseScene):
    player = None
    bullets = []

    def __init__(self, game):
        super().__init__(game)
        self.back_ground = BackGround("resource/bg.jpg")

        player = Player()
        self.player = player

        self.cool_down = 0

        self.register_action(pygame.K_LEFT, player.move_left)
        self.register_action(pygame.K_RIGHT, player.move_right)
        self.register_action(pygame.K_UP, player.move_up)
        self.register_action(pygame.K_DOWN, player.move_down)
        self.register_action(pygame.K_SPACE, self.fire)
        self.register_action(pygame.K_ESCAPE, sys.exit)
        self.draw(player=player)

    def update(self):
        pass

    def fire(self):
        pos = self.player.position()
        bullet = Bullet(pos)
        self.bullets.append(bullet)

    def draw(self, **kwargs):
        self.draw_image(self.back_ground)
        for i in kwargs.keys():
            if i == 'player':
                self.player = kwargs.get(i)

        player = self.player
        self.draw_image(player)

        bullets_temps = []
        for i in self.bullets:
            if i.valid:
                self.draw_image(i)
                i.fire()
                i.move()
                bullets_temps.append(i)
        self.bullets = bullets_temps
